﻿using System;
using System.Web.Security;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web;
using System.Configuration;

namespace Webchatting.Common
{
    public sealed class CommonUtility
    {
        #region 构造函数
        private CommonUtility()
        {

        }
        #endregion

        #region MD5加密
        /// <summary>
        /// 
        /// </summary>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public static string EncodeUsingMD5(string encodeValue)   
        {
            ////获取要加密的字段，并转化为Byte[]数组 
            //byte[] data = System.Text.Encoding.Unicode.GetBytes(pwd.ToCharArray());
            ////建立加密服务 
            //System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            ////加密Byte[]数组 
            //byte[] result = md5.ComputeHash(data);
            ////将加密后的数组转化为字段 
            //string sResult = System.Text.Encoding.Unicode.GetString(result);
            //作为密码方式加密 并返回加密后的值
            return FormsAuthentication.HashPasswordForStoringInConfigFile(encodeValue, "MD5").ToLower();
        }
        #endregion

        #region 字符检测
        public static bool SafeCheckByParams<T>(T param, ref int tryParseInt)
        {
            bool isSafe = false;

            if (param != null)
            {
                isSafe = Int32.TryParse(param.ToString().Trim(), out tryParseInt);
            }
            return isSafe;
        }

        public static bool SafeCheckByParams<T>(T paramsOne, T paramsTwo, ref int tryParseIntFid, ref int tryParseIntCid)
        {
            bool isSafe = false;
            if (paramsOne != null && paramsTwo != null)
            {
                isSafe = Int32.TryParse(paramsOne.ToString().Trim(), out tryParseIntFid) && Int32.TryParse(paramsTwo.ToString().Trim(), out tryParseIntCid);
            }
            return isSafe;
        }

        public static bool SafeCheckByParams<T>(T paramsOne, T paramsTwo, T paramsThree, ref int tryParseIntFid, ref int tryParseIntCid, ref int tryParseIntId)
        {
            bool isSafe=false;
            if (paramsOne != null && paramsTwo != null && paramsThree != null)
            {
                isSafe = Int32.TryParse(paramsOne.ToString().Trim(), out tryParseIntFid) && Int32.TryParse(paramsTwo.ToString().Trim(), out tryParseIntCid) && Int32.TryParse(paramsThree.ToString().Trim(), out tryParseIntId);
            }
            return  isSafe;
        }
        #endregion

        #region 文件真实类型检查
        //public static bool IsAllowedExtension(FileUpload hifile)
        //{
        //    System.IO.FileStream fs = new System.IO.FileStream(HttpContext.Current.Server.MapPath(hifile.PostedFile.FileName), System.IO.FileMode.Open, System.IO.FileAccess.Read);
        //    System.IO.BinaryReader r = new System.IO.BinaryReader(fs);
        //    string fileclass = "";
        //    byte buffer;
        //    try
        //    {
        //        buffer = r.ReadByte();
        //        fileclass = buffer.ToString();
        //        buffer = r.ReadByte();
        //        fileclass += buffer.ToString();

        //    }
        //    catch
        //    {
        //        HttpContext.Current.Response.Write("<script>alert('您选择的图片文件有类型错误,请重新选择！');history.go(-1);</script>");
        //    }
        //    r.Close();
        //    fs.Close();
        //    //说明255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar
        //    if (fileclass == "255216" || fileclass == "7173" || fileclass == "6677" || fileclass == "13780")
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }

        //}
        #endregion

        #region 上传文件后缀名检测
        public static bool IsAllowedExtension(FileUpload hifile)
        {
            string strOldFilePath = "", strExtension = "";
            string[] arrExtension = { ".gif", ".jpg", ".jpeg", ".bmp", ".png" };
            if (string.IsNullOrEmpty(hifile.PostedFile.FileName))
            {
                strOldFilePath = hifile.PostedFile.FileName;
                strExtension = strOldFilePath.Substring(strOldFilePath.LastIndexOf("."));
                for (int i = 0; i < arrExtension.Length; i++)
                {
                    if (strExtension.Equals(arrExtension[i]))
                    {
                        return true;
                    }
                }
            }
            return false;
        }
        #endregion

       
    }
}
